<script lang="ts" setup name="XtxDialog">
import { onClickOutside } from '@vueuse/core'
import { ref, watch } from 'vue'
const props = defineProps({
  title: {
    type: String,
    default: '默认标题'
  },
  visible: {
    type: Boolean,
    default: false
  }
})
// 子传父 通知父组件 修改 visible 为false 
const emit = defineEmits<{
  (e: 'update:visible', v: boolean): void
}>()
const close = () => {
  emit('update:visible', false)
}
// 点击wrapper容器之外 调用close 关闭对话框
const target = ref(null)
onClickOutside(target, () => {
  close()
})

// 重新搞一个布尔值 让这个布尔值和visible同时变化
const show = ref(false)
// 侦听 visible的变化
watch(() => props.visible, (val) => {
  // 单独开一个宏任务 再去设置show的变化
  setTimeout(() => {
    show.value = val
  }, 0)
})
</script>
<template>
  <!-- 样式的方式 隐藏  通过添加类名  显示 -->
  <div class="xtx-dialog" v-if="visible" :class="{ fade: show }">
    <div class="wrapper" ref="target" :class="{ fade: show }">
      <div class="header">
        <h3>{{ title }}</h3>
        <a href="JavaScript:;" class="iconfont icon-close-new" @click="close"></a>
      </div>
      <div class="body">
        <!-- 主题内容的插槽 -->
        <slot></slot>
      </div>
      <div class="footer">
        <!-- 底部按钮的区域的插槽 -->
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-dialog {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8887;

  background: rgba(0, 0, 0, 0);

  &.fade {
    transition: all 0.4s;
    background: rgba(0, 0, 0, 0.5);
  }

  .wrapper {
    width: 600px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;

    transform: translate(-50%, -100%);
    opacity: 0;

    &.fade {
      transition: all 0.4s;
      transform: translate(-50%, -50%);
      opacity: 1;
    }

    .body {
      padding: 20px 40px;
      font-size: 16px;

      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
    }

    .footer {
      text-align: center;
      padding: 10px 0 30px 0;
    }

    .header {
      position: relative;
      height: 70px;
      line-height: 70px;
      padding: 0 20px;
      border-bottom: 1px solid #f5f5f5;

      h3 {
        font-weight: normal;
        font-size: 18px;
      }

      a {
        position: absolute;
        right: 25px;
        top: 25px;
        font-size: 24px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;

        &:hover {
          color: #666;
        }
      }
    }
  }
}
</style>